#!/bin/bash

set -o errexit -o nounset -o pipefail

TEST_DIRS="
./build/demos
./build-x86_64/demos
./build-i686/demos
./build-win32/demos/Debug
./build-x64/demos/Debug
"

# Straightforward and ugly for now: given a test name, look for everywhere we
# could have built it, and run what we find. Exit on first failure.
# Usage:
#   run_test <test_name>
run_test() {
  local test_name=$1

  for test_dir in $TEST_DIRS; do
    test_bin=${test_dir}/${test_name}

    if [[ -x "${test_bin}.exe" ]]; then
      test_bin=${test_bin}.exe
    fi

    if [[ -x "${test_bin}" ]]; then
      echo "== BEGIN    ${test_bin}"
      TIMEFORMAT='== END      %2R seconds'
      time "${test_bin}" || (
        echo "== FAILED   return code $?"
        exit 1
      )
      echo
    fi
  done
}

run_test timing_array_test
run_test spectre_v1_pht_sa
run_test faults_test
